<template>
	<view>
		
		
		<form @submit="onSubmit" @reset="">
					
					
					
					<view class="form-item">
						<text>收件人</text>
						<input type="text" name="name" :value="addressInfo.name" placeholder="收货人姓名">
					</view>
					
					<view class="form-item">
						<text>手机号</text>
						<input type="tel" name="tel" :value="addressInfo.tel" placeholder="收货人手机号">
					</view>
					
					<view class="form-item" @click="chooseArea">
						<text>所在地</text>
						<view class="item-content">
							<view style="">{{pickerText == '' ? '选择省/市/区(县)' : pickerText}}</view>
						</view>
						<view class="icon">
							<text class="iconfont icon-ziyuan3"></text>
						</view>
					</view>
					
					<view class="form-item">
						<text>详情地址</text>
						<input type="text" name="addressDetail" :value="addressInfo.addressDetail" placeholder="街道门牌/楼层房间号等信息">
					</view>
					
					
					
					<button form-type="submit" class="submit">提交</button>
				</form>
				
				
				
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor="#007AFF"></simple-address>
		
		
	</view>
</template>

<script>
	
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	import common from '../../common.js'
	
	export default {
		
		components: {
			simpleAddress
		},
		
		data() {
			return {
				address_id: 0,
				addressInfo : {
					name : '',
					tel : '',
					addressDetail : ''					
				},
				cityPickerValueDefault: [0, 0, 1],
				pickerText: '',
				labelArr: ["", "", ""]
			};
		},
		
		onLoad(options) {		
			 
			
			this.loadData(options.id)
			
			this.address_id = options.id
						 
		},
		methods: {
			
			
			
			onSubmit(e) {
				let content = e.detail.value;
			
				if (content.name === '') {
					uni.showToast({
						title: '姓名不能为空'
					});
					return false;
				}
			
				// 省市区
				content.province = this.labelArr[0];
				content.city = this.labelArr[1];
				content.county = this.labelArr[2];
				
				// address_id有这个字段 就是更新
				content.address_id = this.address_id
			
				this.$http.post('/shop.address/create', content).then(res => {
					
					if (res.data.code === 0) {
						
						uni.navigateBack({
							url: './address'
						});
						
						// Toast('收货地址添加成功');
						// this.$EventBus.$emit('address_update_success', 'ok');
						//this.backWhere();
						// this.$router.push("/user/address");
					}
				});
			},
			
			chooseArea() {
				
				// 默认加载省市区				
				if (common.isEmpty(this.addressInfo.province) || common.isEmpty(this.addressInfo.city) || common.isEmpty(this.addressInfo.county) ){					
					// this.cityPickerValueDefault = index.index;
					this.cityPickerValueDefault = [0,0,1];					  
				}else{
					var index2 =  this.$refs.simpleAddress.queryIndex([this.addressInfo.province, this.addressInfo.city, this.addressInfo.county], 'label');
					this.cityPickerValueDefault = index2.index;
				}

				this.$refs.simpleAddress.open();				
			},			
			onConfirm(e) {
				console.log(JSON.stringify(e));
				this.pickerText = e.label;
				this.labelArr = e.labelArr;
			},
			
			
			loadData(id) {
				this.$http.post('/shop.address/read', {
				    address_id: id
				}).then(res => {				
				
				  this.addressInfo.name = res.data.data.name
				  this.addressInfo.tel = res.data.data.phone				  
				  this.addressInfo.addressDetail = res.data.data.detail
				  
				  // 省市县
				  this.addressInfo.province  = res.data.data.province + ''
				  this.addressInfo.city = res.data.data.city + ''
				  this.addressInfo.county = res.data.data.county + ''
				
				
				
				this.labelArr[0] = this.addressInfo.province
				this.labelArr[1] = this.addressInfo.city
				this.labelArr[2] = this.addressInfo.county
				
				this.pickerText = this.addressInfo.province  + '-' + this.addressInfo.city + '-' + this.addressInfo.county
					
				
				});
			}
		},
	}
</script>


<style lang="scss">

.submit {
	background-color: $colorPrimary;
	color: white;
	border-radius: 0;
	margin-top: 30rpx;
}
</style>